package com.qingcloud.adminbackend.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qingcloud.adminbackend.constant.WamingConstant;
import com.qingcloud.adminbackend.entity.upgrade.*;
import com.qingcloud.adminbackend.exception.WmCommonException;
import com.qingcloud.adminbackend.base.AjaxResult;
import com.qingcloud.adminbackend.entity.aline.BaseTcpCommandDTO;
import com.qingcloud.adminbackend.entity.aline.CommandAdapter;
import com.qingcloud.adminbackend.entity.aline.TcpValueCommandDTO;
import com.qingcloud.adminbackend.entity.aline.adapters.ABaseTcpCommandAdapter;
import com.qingcloud.adminbackend.entity.aline.adapters.BaseQueryTcpCommandAdapter;
import com.qingcloud.adminbackend.entity.command.CommandRecord;
import com.qingcloud.adminbackend.entity.config.SysCommonConfig;
import com.qingcloud.adminbackend.entity.equipment.EquipmentBaseDataDTO;
import com.qingcloud.adminbackend.entity.upgrade.*;
import com.qingcloud.adminbackend.entity.upgrade.commandResult.Command32;
import com.qingcloud.adminbackend.entity.upgrade.commandResult.Command34;
import com.qingcloud.adminbackend.entity.upgrade.commandResult.Command35;
import com.qingcloud.adminbackend.entity.upgrade.commandResult.Command36;
import com.qingcloud.adminbackend.mapper.ITdCalibrationMapper;
import com.qingcloud.adminbackend.mapper.SysCommonConfigMapper;
import com.qingcloud.adminbackend.service.ITdCalibrationService;
import com.qingcloud.adminbackend.utils.DateUtil;
import com.qingcloud.adminbackend.utils.OKHttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName TdCalibrationServiceImpl
 * @Description 设备校准服务实现类
 * @Author qym
 * @Date 2020/12/22 下午12:50
 **/
@Service
public class ITdCalibrationServiceImpl implements ITdCalibrationService {
    private static Logger logger = LoggerFactory.getLogger(ITdCalibrationServiceImpl.class);

    @Resource
    private ITdCalibrationMapper calibrationMapper;

    @Resource
    private SysCommonConfigMapper sysCommonConfigMapper;

    private static final String GRID_COMMON_CONFIG_KEY = "station_command_url";//室内设备参数下发url地址


    @Value("${healthy.data-url}")
    private String crawlerUrl;

    @Override
    public AjaxResult getCalibrationEventPage(CalibrationRequest calibrationRequest) {
        Page<CalibrationEventDTO> page = new Page<>(calibrationRequest.getPage(), calibrationRequest.getPerPage());
        IPage<CalibrationEventDTO> calibrationEventDTOS = calibrationMapper.searchEvent(page, calibrationRequest);
        List<Integer> eventIds = calibrationEventDTOS.getRecords().stream().map(CalibrationEventDTO::getId).collect(Collectors.toList());
        if (eventIds.size() == 0) return new AjaxResult(WamingConstant.SUCCESS, "查询成功", new ArrayList<>());
        List<CalibrationEquipmentDTO> equipmentDTOS = calibrationMapper.searchEquipmentByEvent(eventIds);
        calibrationEventDTOS.getRecords().forEach(ev -> {
            equipmentDTOS.forEach(eq -> {
                if (eq.getCalibrationEvent().equals(ev.getId())) {
                    if (eq.getType().equals(0)) {
                        // 标准
                        ev.setStandardEquipmentId(eq.getEquipmentId());
                    } else {
                        // 被校准
                        if (ev.getCalibrationEquipmentIds() == null) {
                            List<String> ids = new ArrayList<>();
                            ids.add(eq.getEquipmentId());
                            ev.setCalibrationEquipmentIds(ids);
                        } else {
                            ev.getCalibrationEquipmentIds().add(eq.getEquipmentId());
                        }
                    }
                }
            });
        });
        return new AjaxResult(WamingConstant.SUCCESS, "查询成功", calibrationEventDTOS);
    }

    @Override
    public AjaxResult searchEquipments(List<String> equipmentIds) {
        List<CalibrationEquipmentDetailDTO> equipmentDTOS = calibrationMapper.searchEquipmentById(equipmentIds);
        return new AjaxResult(WamingConstant.FAILURE, "查询成功", equipmentDTOS);
    }

    @Override
    @Transactional
    public AjaxResult addCalibrationEvent(CalibrationEventDTO calibrationEventDTO) {
        List<String> equipmentIds = calibrationEventDTO.getCalibrationEquipmentIds();
        equipmentIds.add(calibrationEventDTO.getStandardEquipmentId());
        // 查询设备是否已存在
        Long count = calibrationMapper.existEquipment(equipmentIds);
        if (count > 0) {
            return new AjaxResult(WamingConstant.FAILURE, "设备正在校准中", null);
        }
        Integer integer = calibrationMapper.insertEvent(calibrationEventDTO);
        if (integer == null || integer < 1) {
            throw new WmCommonException("新增事件失败");
        }
        Integer maxId = calibrationMapper.findMaxEventId();
        List<CalibrationEquipmentDTO> list = new ArrayList<>();
        CalibrationEquipmentDTO dto = new CalibrationEquipmentDTO();
        dto.setEquipmentId(calibrationEventDTO.getStandardEquipmentId());
        dto.setType(0);
        dto.setStatus(1);
        dto.setCalibrationEvent(maxId);
        list.add(dto);
        calibrationEventDTO.getCalibrationEquipmentIds().forEach(it -> {
            CalibrationEquipmentDTO dto1 = new CalibrationEquipmentDTO();
            dto1.setEquipmentId(it);
            dto1.setType(1);
            dto1.setStatus(1);
            dto1.setCalibrationEvent(maxId);
            list.add(dto1);
        });
        Integer insertFlag = calibrationMapper.insertEquipment(list);
        // 调用命令开始校准
        startCalibration(list.stream().map(CalibrationEquipmentDTO::getEquipmentId).collect(Collectors.toList()));
        if (insertFlag > 0)
            return new AjaxResult(WamingConstant.SUCCESS, "新增成功", null);
        else throw new WmCommonException("新增设备失败");
    }

    @Override
    public AjaxResult addCalibrationEquipment(List<CalibrationEquipmentDTO> calibrationEquipmentDTOs) {
        List<String> equipmentIds = calibrationEquipmentDTOs.stream().map(CalibrationEquipmentDTO::getEquipmentId).collect(Collectors.toList());
        // 查询设备是否已存在
        Long count = calibrationMapper.existEquipment(equipmentIds);
        if (count > 0) {
            return new AjaxResult(WamingConstant.FAILURE, "设备正在校准中", null);
        }
        List<Integer> eventIds = calibrationEquipmentDTOs.stream().map(CalibrationEquipmentDTO::getCalibrationEvent).collect(Collectors.toList());
        // 检查校准事件是否存在
        Long countEvent = calibrationMapper.existEvent(eventIds);
        if (countEvent <= 0) {
            return new AjaxResult(WamingConstant.FAILURE, "校准事件不存在", null);
        }
        // 设置为校准模式
        List<TcpValueCommandDTO> tcpValueCommandDTOList = new ArrayList<>();
        calibrationEquipmentDTOs.forEach(it -> {
            TcpValueCommandDTO dto = new TcpValueCommandDTO();
            dto.setEquipmentId(it.getEquipmentId());
            dto.setCommand(34);
            dto.setValue(1);
        });
        setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
        Integer insertFlag = calibrationMapper.insertEquipment(calibrationEquipmentDTOs);
        if (insertFlag > 0)
            return new AjaxResult(WamingConstant.SUCCESS, "新增成功", null);
        else return new AjaxResult(WamingConstant.FAILURE, "新增失败", null);
    }

    @Override
    public AjaxResult searchCalibrationMode(List<String> equipmentIds) {
        List<CalibrationEquipmentDetailDTO> resultList = new ArrayList<>();
        List<BaseTcpCommandDTO> list = new ArrayList<>();
        equipmentIds.forEach(it -> {
            BaseTcpCommandDTO commandDTO = new BaseTcpCommandDTO();
            commandDTO.setEquipmentId(it);
            commandDTO.setCommand(34);
        });
        List<CommandRecord> commandRecords = command(new BaseQueryTcpCommandAdapter(list));
        if (commandRecords != null && commandRecords.size() > 0) {
            commandRecords.forEach(it -> {
                Command34 command34 = JSON.parseObject(it.getResult(), Command34.class);
                CalibrationEquipmentDetailDTO dto = new CalibrationEquipmentDetailDTO();
                dto.setEquipmentId(it.getEquipmentId());
                dto.setCalibrationMode(String.valueOf(command34.getType()));
                resultList.add(dto);
            });
        }
        return new AjaxResult(WamingConstant.SUCCESS, "查询成功", resultList);
    }

    @Override
    public AjaxResult setCalibrationMode(List<TcpValueCommandDTO> tcpValueCommandDTOList) {
        tcpValueCommandDTOList.forEach(it -> {
            it.setCommand(34);
        });
        return setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @Override
    public AjaxResult searchCalibrationTamper(List<String> equipmentIds) {
        List<CalibrationEquipmentDetailDTO> resultList = new ArrayList<>();
        List<BaseTcpCommandDTO> list = new ArrayList<>();
        equipmentIds.forEach(it -> {
            BaseTcpCommandDTO commandDTO = new BaseTcpCommandDTO();
            commandDTO.setEquipmentId(it);
            commandDTO.setCommand(32);
        });
        List<CommandRecord> commandRecords = command(new BaseQueryTcpCommandAdapter(list));
        if (commandRecords != null && commandRecords.size() > 0) {
            commandRecords.forEach(it -> {
                Command32 command = JSON.parseObject(it.getResult(), Command32.class);
                CalibrationEquipmentDetailDTO dto = new CalibrationEquipmentDetailDTO();
                dto.setEquipmentId(it.getEquipmentId());
                dto.setStatusTamper(command.getStatus());
                dto.setTimesTamper(command.getTimes());
                resultList.add(dto);
            });
        }
        return new AjaxResult(WamingConstant.SUCCESS, "查询成功", resultList);
    }

    @Override
    public AjaxResult setCalibrationTamper(List<TcpValueCommandDTO> tcpValueCommandDTOList) {
        tcpValueCommandDTOList.forEach(it -> {
            it.setCommand(34);
        });
        return setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @Override
    public AjaxResult setCalibrationStep(CalibrationStepDTO calibrationStepDTO) {
        Command35 command = new Command35();
        command.setAlineStep1(calibrationStepDTO.getStep1());
        command.setAlineStep2(calibrationStepDTO.getStep2());
        command.setAlineStep3(calibrationStepDTO.getStep3());
        command.setAlineStep4(calibrationStepDTO.getStep4());
        command.setAlineStep5(calibrationStepDTO.getStep5());
        command.setAlineStep6(calibrationStepDTO.getStep6());
        command.setAlineStep7(calibrationStepDTO.getStep7());
        command.setAlineStep8(calibrationStepDTO.getStep8());
        List<TcpValueCommandDTO> tcpValueCommandDTOList = new ArrayList<>();
        calibrationStepDTO.getEquipmentIds().forEach(it -> {
            TcpValueCommandDTO dto = new TcpValueCommandDTO();
            dto.setEquipmentId(it);
            dto.setCommand(35);
            dto.setValue(command);
            tcpValueCommandDTOList.add(dto);
        });
        return setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @Override
    public AjaxResult searchCalibrationStep(List<String> equipmentIds) {
        List<CalibrationStepDTO> resultList = new ArrayList<>();
        List<BaseTcpCommandDTO> list = new ArrayList<>();
        equipmentIds.forEach(it -> {
            BaseTcpCommandDTO commandDTO = new BaseTcpCommandDTO();
            commandDTO.setEquipmentId(it);
            commandDTO.setCommand(35);
        });
        List<CommandRecord> commandRecords = command(new BaseQueryTcpCommandAdapter(list));
        if (commandRecords != null && commandRecords.size() > 0) {
            commandRecords.forEach(it -> {
                Command35 command35 = JSON.parseObject(it.getResult(), Command35.class);
                CalibrationStepDTO dto = new CalibrationStepDTO();
                dto.setEquipmentId(it.getEquipmentId());
                dto.setStep1(command35.getAlineStep1());
                dto.setStep2(command35.getAlineStep2());
                dto.setStep3(command35.getAlineStep3());
                dto.setStep4(command35.getAlineStep4());
                dto.setStep5(command35.getAlineStep5());
                dto.setStep6(command35.getAlineStep6());
                dto.setStep7(command35.getAlineStep7());
                dto.setStep8(command35.getAlineStep8());
                resultList.add(dto);
            });
        }
        return new AjaxResult(WamingConstant.SUCCESS, "查询成功", resultList);
    }

    @Override
    public AjaxResult getNewestData(List<String> equipmentIds) {
        List<CalibrationEquipmentDataDTO> calibrationEquipmentDataDTOS = new ArrayList<>();
        List<CalibrationEquipmentDetailDTO> calibrationEquipmentDetailDTOS = calibrationMapper.searchEquipmentById(equipmentIds);
        // 查询每个id的最新一条数据
        equipmentIds.forEach(id -> {
            JSONObject param = new JSONObject();
            param.put("dateType", "1");
            param.put("endTime", DateUtil.getCurrentDateTime());
            param.put("equipmentIdList", Collections.singletonList(id));
            param.put("startTime", DateUtil.getCurrentDateTime());
            param.put("length", 1);
            param.put("page", 0);
            //发起请求
            String result = OKHttpUtil.httpPost(crawlerUrl+"edata/page", param.toJSONString());
            //结束返回参数
            Map<String, Object> resultMap = (Map<String, Object>) JSONObject.parse(result);
            List<EquipmentBaseDataDTO> baseDataDTOS = JSONObject.parseArray(String.valueOf(resultMap.get("data")), EquipmentBaseDataDTO.class);
            calibrationEquipmentDetailDTOS.forEach(de -> {
                CalibrationEquipmentDataDTO dto = new CalibrationEquipmentDataDTO();
                dto.setEquipmentId(de.getEquipmentId());
                dto.setType(de.getType());
                baseDataDTOS.forEach(it -> {
                    if (de.getEquipmentId().equalsIgnoreCase(it.getEquipmentId())) {
                        dto.setInsTime(it.getRtcTime());
                        dto.setScatteredParticles1(it.getAerosol1());
                        dto.setScatteredParticles2(it.getAerosol2());
                        dto.setScatteredParticles3(it.getAerosol3());
                        dto.setScatteredParticles4(it.getAerosol4());
                        dto.setFluorescentParticles1(it.getAerosol5());
                        dto.setFluorescentParticles2(it.getAerosol6());
                        dto.setFluorescentParticles3(it.getAerosol7());
                        dto.setFluorescentParticles4(it.getAerosol8());
                    }
                });
                calibrationEquipmentDataDTOS.add(dto);
            });
        });
        return new AjaxResult(WamingConstant.SUCCESS, "查询成功", calibrationEquipmentDataDTOS);
    }

    @Override
    public AjaxResult searchCalibrationDAC(String equipmentId) {
        CalibrationEquipmentDetailDTO dto = new CalibrationEquipmentDetailDTO();
        List<BaseTcpCommandDTO> list = new ArrayList<>();
        BaseTcpCommandDTO commandDTO = new BaseTcpCommandDTO();
        commandDTO.setEquipmentId(equipmentId);
        commandDTO.setCommand(28);
        list.add(commandDTO);
        BaseTcpCommandDTO commandDTO1 = new BaseTcpCommandDTO();
        commandDTO1.setEquipmentId(equipmentId);
        commandDTO1.setCommand(31);
        list.add(commandDTO1);
        List<CommandRecord> commandRecords = command(new BaseQueryTcpCommandAdapter(list));
        if (commandRecords != null && commandRecords.size() > 0) {
            commandRecords.forEach(it -> {
                if (it.getCmdCode().equalsIgnoreCase("28")) {
                    dto.setDAC_PhotoelectricPlate(it.getResult());
                }
                if (it.getCmdCode().equalsIgnoreCase("31")) {
                    dto.setDAC_PMT(it.getResult());
                }
            });
        }
        return new AjaxResult(WamingConstant.SUCCESS, "查询成功", dto);
    }

    @Override
    public AjaxResult setCalibrationAccuracy(CalibrationAccuracyDTO calibrationAccuracyDTO) {
        Command36 command36 = new Command36();
        command36.setAlineJd1(calibrationAccuracyDTO.getAccuracy1());
        command36.setAlineJd2(calibrationAccuracyDTO.getAccuracy2());
        command36.setAlineJd3(calibrationAccuracyDTO.getAccuracy3());
        command36.setAlineJd4(calibrationAccuracyDTO.getAccuracy4());
        command36.setAlineJd5(calibrationAccuracyDTO.getAccuracy5());
        command36.setAlineJd6(calibrationAccuracyDTO.getAccuracy6());
        command36.setAlineJd7(calibrationAccuracyDTO.getAccuracy7());
        command36.setAlineJd8(calibrationAccuracyDTO.getAccuracy8());
        List<TcpValueCommandDTO> tcpValueCommandDTOList = new ArrayList<>();
        calibrationAccuracyDTO.getEquipmentIds().forEach(it -> {
            TcpValueCommandDTO dto = new TcpValueCommandDTO();
            dto.setEquipmentId(it);
            dto.setCommand(36);
            dto.setValue(command36);
            tcpValueCommandDTOList.add(dto);
        });
        return setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
    }

    @Override
    public AjaxResult searchCalibrationAccuracy(List<String> equipmentIds) {
        List<CalibrationAccuracyDTO> resultList = new ArrayList<>();
        List<BaseTcpCommandDTO> list = new ArrayList<>();
        equipmentIds.forEach(it -> {
            BaseTcpCommandDTO commandDTO = new BaseTcpCommandDTO();
            commandDTO.setEquipmentId(it);
            commandDTO.setCommand(36);
        });
        List<CommandRecord> commandRecords = command(new BaseQueryTcpCommandAdapter(list));
        if (commandRecords != null && commandRecords.size() > 0) {
            commandRecords.forEach(it -> {
                Command36 command36 = JSON.parseObject(it.getResult(), Command36.class);
                CalibrationAccuracyDTO dto = new CalibrationAccuracyDTO();
                dto.setEquipmentId(it.getEquipmentId());
                dto.setAccuracy1(command36.getAlineJd1());
                dto.setAccuracy2(command36.getAlineJd2());
                dto.setAccuracy3(command36.getAlineJd3());
                dto.setAccuracy4(command36.getAlineJd4());
                dto.setAccuracy5(command36.getAlineJd5());
                dto.setAccuracy6(command36.getAlineJd6());
                dto.setAccuracy7(command36.getAlineJd7());
                dto.setAccuracy8(command36.getAlineJd8());
                resultList.add(dto);
            });
        }
        return new AjaxResult(WamingConstant.SUCCESS, "查询成功", resultList);
    }

    @Override
    public AjaxResult stopCalibration(List<String> equipmentIds) {
        List<CalibrationEquipmentDetailDTO> detailDTOS = calibrationMapper.searchEquipmentById(equipmentIds);
        List<TcpValueCommandDTO> tcpValueCommandDTOList = new ArrayList<>();
        detailDTOS.forEach(it -> {
            TcpValueCommandDTO dto = new TcpValueCommandDTO();
            dto.setValue(0);
            dto.setEquipmentId(it.getEquipmentId());
            dto.setCommand(34);
            tcpValueCommandDTOList.add(dto);
        });
        AjaxResult ajaxResult = setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {
            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
        calibrationMapper.updateEquipmentStatus(equipmentIds, "0");
        return ajaxResult;
    }

    @Override
    public AjaxResult stopAllCalibration(List<Integer> eventIds) {

        List<String> equipmentIds = calibrationMapper.searchEquipmentByEvent(eventIds)
                .stream().map(CalibrationEquipmentDTO::getEquipmentId).collect(Collectors.toList());
        List<CalibrationEquipmentDetailDTO> detailDTOS = calibrationMapper.searchEquipmentById(equipmentIds);
        List<TcpValueCommandDTO> tcpValueCommandDTOList = new ArrayList<>();
        detailDTOS.forEach(it -> {
            TcpValueCommandDTO dto = new TcpValueCommandDTO();
            dto.setValue(0);
            dto.setEquipmentId(it.getEquipmentId());
            dto.setCommand(34);
            tcpValueCommandDTOList.add(dto);
        });
        AjaxResult ajaxResult = setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {
            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
        // 停止校准更新数据库
        calibrationMapper.updateEquipmentStatus(equipmentIds, "0");
        calibrationMapper.updateEventStatus(equipmentIds, "0");
        calibrationMapper.deleteEquipment(equipmentIds);
        return ajaxResult;
    }

    @Override
    public AjaxResult startCalibration(List<String> equipmentIds) {
        List<CalibrationEquipmentDetailDTO> detailDTOS = calibrationMapper.searchEquipmentById(equipmentIds);
        List<TcpValueCommandDTO> tcpValueCommandDTOList = new ArrayList<>();
        detailDTOS.forEach(it -> {
            TcpValueCommandDTO dto = new TcpValueCommandDTO();
            if (it.getType() == 1) {
                dto.setValue(1);// 被校准设备
            } else if (it.getType() == 0) {
                dto.setValue(2);// 标准设备
            }
            dto.setEquipmentId(it.getEquipmentId());
            dto.setCommand(34);
            tcpValueCommandDTOList.add(dto);
        });
        AjaxResult ajaxResult = setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {
            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
        if (ajaxResult.getErroCode() == WamingConstant.SUCCESS)
            calibrationMapper.updateEquipmentStatus(equipmentIds, "1");
        return ajaxResult;
    }

    @Override
    public AjaxResult setAllUpLoadFeq(CalibrationRequest request) {
        List<TcpValueCommandDTO> tcpValueCommandDTOList = new ArrayList<>();
        TcpValueCommandDTO dto = new TcpValueCommandDTO();
        dto.setEquipmentId(request.getEquipmentId());
        dto.setCommand(4);
        dto.setValue(request.getStandardPhotoelectricPlate());
        TcpValueCommandDTO dto1 = new TcpValueCommandDTO();
        dto1.setEquipmentId(request.getEquipmentId());
        dto1.setCommand(29);
        dto1.setValue(request.getFreqPhotoelectricPlate());
        tcpValueCommandDTOList.add(dto);
        return setCommand(new ABaseTcpCommandAdapter<List<TcpValueCommandDTO>>(tcpValueCommandDTOList) {

            @Override
            public String getUrl(String baseUrl) {
                return baseUrl + WamingConstant.TCP_BATCH_SET_COMMAND;
            }
        });
    }

    private List<CommandRecord> command(CommandAdapter adapter) {
        SysCommonConfig sysCommonConfig = sysCommonConfigMapper.findByKey(GRID_COMMON_CONFIG_KEY);
        String url = adapter.getUrl(sysCommonConfig.getValue());
        try {
            String paramJson = adapter.toReceiveJson();
            //基础指令查询下发
            logger.info("指令地址：" + url);
            logger.info("指令参数：" + paramJson);
            String resultStr = OKHttpUtil.httpPost(url, paramJson);
            logger.info("指令查询结果:" + resultStr);
            final AjaxResult<List<CommandRecord>> tcpResultDto = JSON.parseObject(resultStr, AjaxResult.class);
            if (tcpResultDto != null && tcpResultDto.getErroCode() != 0) {
                if (WamingConstant.SUCCESS == tcpResultDto.getErroCode()) {
                    tcpResultDto.setErroMsg("下发成功");
                }
                return tcpResultDto.getResult();
            } else
                return null;
        } catch (Exception e) {
            logger.error("指令查询下发 error, {}", e);
            return null;
        }
    }

    private AjaxResult setCommand(CommandAdapter adapter) {
        SysCommonConfig sysCommonConfig = sysCommonConfigMapper.findByKey(GRID_COMMON_CONFIG_KEY);
        String url = adapter.getUrl(sysCommonConfig.getValue());
        try {
            String paramJson = adapter.toReceiveJson();
            //基础指令查询下发
            logger.info("指令地址：" + url);
            logger.info("指令参数：" + paramJson);
            String resultStr = OKHttpUtil.httpPost(url, paramJson);
            logger.info("指令设置结果:" + resultStr);
            return handlerCmdResult(resultStr);
        } catch (Exception e) {
            logger.error("指令设置下发 error, {}", e);
            return new AjaxResult(WamingConstant.FAILURE, "设置失败", null);
        }
    }

    private AjaxResult handlerCmdResult(String resultStr) {
        final AjaxResult<CommandRecord> tcpResultDto = JSON.parseObject(resultStr, AjaxResult.class);
        if (tcpResultDto != null && tcpResultDto.getErroCode() != 0) {
            if (WamingConstant.SUCCESS == tcpResultDto.getErroCode()) {
                tcpResultDto.setErroMsg("设置成功");
            }
            return tcpResultDto;
        } else
            return new AjaxResult(WamingConstant.FAILURE, "设置失败", null);
    }
}
